import { _decorator, BoxCollider2D, Component, Contact2DType, EventTouch, instantiate, Node, Prefab, RigidBody2D, UITransform, Vec2, Vec3 } from 'cc';
import { PbulletManager } from './PbulletManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerManager')
export class PlayerManager extends Component {
    //判断是否移动
    private ismove: boolean = false;
    //卡牌原来位置
    private cardPos: Vec2 | null = null;
    //鼠标开始移动的位置
    private mouseBeginPos: Vec3 | null = null;
    //鼠标移动的位置
    private mouseMovePos: Vec3 | null = null;
    //卡牌的层级
    cardCengji: number = 0;    
    isDestroy: boolean = false;
    @property(Prefab)
    pbullet:Prefab

    
    playerColliderBox:BoxCollider2D
    start() {

    }

    protected onLoad(): void {
        let bullet = instantiate(this.pbullet);
        bullet.getComponent(PbulletManager)
        this.node.addChild(bullet);

        this.node.off(Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.off(Node.EventType.TOUCH_END, this.touchEnd, this);

        this.node.on(Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.touchEnd, this);  
        this.BoxColliderListener()
    }
    protected update(dt: number): void {
        if(this.isDestroy){
            let bullet = instantiate(this.pbullet);
            bullet.getComponent(PbulletManager)
            this.node.addChild(bullet);
            this.isDestroy = false;
        }
    }
    touchStart(e:EventTouch) {
        console.log("touchStart");
        this.cardPos =new Vec2(this.node.getPosition().x,this.node.getPosition().y) ;
        this.mouseBeginPos=new Vec3(e.getLocation().x,e.getLocation().y,0); 
        this.cardCengji=this.node.getSiblingIndex()
        let addcengji=this.cardCengji+100;
        this.node.setSiblingIndex(addcengji)        
        this.ismove = true;      
    }
    
    touchEnd() {
        console.log("touchEnd");  
        this.ismove = false;
        this.node.getComponent(UITransform).width=100;
        this.node.getComponent(UITransform).height=100;
        this.node.setPosition(this.cardPos.x,this.cardPos.y,0);
        this.node.setSiblingIndex(this.cardCengji)
  
    }
    touchMove(e: EventTouch) {
        console.log("touchMove");
        if(this.ismove){
            //根据世界坐标来设置移动位置         
            this.mouseMovePos=new Vec3(e.getLocation().x,e.getLocation().y,0);
            let beforPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(this.mouseBeginPos);
            let localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(this.mouseMovePos);
            let dtx = localPos.x - beforPos.x;
            let dty = localPos.y - beforPos.y;
            this.node.setPosition(this.cardPos.x+dtx, this.cardPos.y+dty);
        }
    }

    BoxColliderListener(){       
        console.log("BoxColliderListener:")
        this.playerColliderBox= this.node.getComponent(BoxCollider2D)
        this.playerColliderBox.on(Contact2DType.BEGIN_CONTACT,this.onCollisionEnter,this)
    }
    //调用碰撞方法
    onCollisionEnter(){
        console.log("碰撞了",this.playerColliderBox.group)
    }
}

